package com.xuequ.answerme.main;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xuequ.answerme.R;
import com.xuequ.answerme.store.FocusViewPagerFragment;
import com.xuequ.answerme.store.LikeViewPagerFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Shijie on 2015/9/30.
 * <p/>
 * storeFragment shows user the questions the user has focused on and the answers the user likes.
 */
public class StoreFragment extends Fragment {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private TabFragmentAdapter fragmentAdapter;

    private List<String> tabTitlesList;
    private List<Fragment> fragmentsList;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_store, container, false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {

        mViewPager = (ViewPager) view.findViewById(R.id.store_viewPager);
        mTabLayout = (TabLayout) view.findViewById(R.id.store_tabLayout);

        tabTitlesList = new ArrayList<>();
        fragmentsList = new ArrayList<>();

        tabTitlesList.add("Focus");
        tabTitlesList.add("Like");

        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitlesList.get(0)));
        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitlesList.get(1)));

        fragmentsList.add(new FocusViewPagerFragment());
        fragmentsList.add(new LikeViewPagerFragment());

        fragmentAdapter = new TabFragmentAdapter(getFragmentManager(), fragmentsList, tabTitlesList);
        mViewPager.setAdapter(fragmentAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(fragmentAdapter);

    }

    public void callToRefreshData() {
        if (mViewPager.getCurrentItem() == 0) {
            FocusViewPagerFragment focusViewPagerFragment = (FocusViewPagerFragment) fragmentAdapter.getItem(0);
            focusViewPagerFragment.callToRefreshData();
        }
        if (mViewPager.getCurrentItem() == 1) {
            LikeViewPagerFragment likeViewPagerFragment = (LikeViewPagerFragment) fragmentAdapter.getItem(1);
            likeViewPagerFragment.callToRefreshData();
        }
    }

    public class TabFragmentAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> mFragments;
        private List<String> mTitles;

        public TabFragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
            super(fm);
            mFragments = fragments;
            mTitles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles.get(position);
        }
    }
}

